package com.kaifamiao.jackson;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.kaifamiao.enums.Gender;

import java.io.IOException;

public class GenderDeserializer extends JsonDeserializer<Gender> {

    @Override
    public Gender deserialize(JsonParser parser, DeserializationContext context) throws IOException, JacksonException {
        String text = parser.getText(); // 获得客户端传递过来的值
        if( text == null || text.trim().isEmpty() ) {
            return Gender.UNKNOWN;
        }
        String enumConstantName = text.toUpperCase(); // 将接收到的字符串转换为大写英文字母
        return Gender.valueOf(enumConstantName); // 根据 枚举常量的名称 返回相应的 枚举常量
    }

}
